import plugin from '../../../lib/plugins/plugin.js'
import fs from "fs";
import lodash from "lodash";
import { Config } from '../components/index.js'
import { YamlReader , puppeteer , common} from '../model/index.js';
const configs = {
    "戳一戳": "chuochuo",
    "戳一戳语音": "chuorecord",
    "戳一戳接口": "chuoapi",
    "pluscd": "pluscd",
    "用户源": 'plususer',
    "默认帮助": 'defaulthelp',
    "解析": 'jiexi',
    "哔哩解析" : 'bljx',
    '群管' : 'qg',
    'ai' : 'ai'
}

let managereg = new RegExp(`^#?小月设置(${Object.keys(configs).join("|")})(开启|关闭|\\d+)$`)
export class NewConfig extends plugin {
    constructor() {
        super({
            name: '小月配置',
            event: 'message',
            priority: 100,
            rule: [
                {
                    reg: managereg,
                    fnc: 'Config_manage',
                    permission: 'master'
                },
                {
                    reg: '^#?小月设置$',
                    fnc: 'xiaoyueset',
                    permission: 'master'
                },
            ]
        })
    }
    

    //渲染发送图片
    async xiaoyueset(e) {
        let config = await Config.xyss
        let data = {
            yxy: getStatus(config.yxy),
            yxychcd:  `<div class="cfg-status" >${config.yxychcd}</div>`,
            chuochuo: getStatus(config.chuochuo),
            chuorecord: getStatus(config.chuorecord),
            chuoapi: getStatus(config.chuoapi),
            pluscd:  `<div class="cfg-status" >${config.pluscd}</div>`,
            plususer: getStatus(config.plususer),
            defaulthelp: getStatus(config.defaulthelp),
            jiexi: getStatus(config.jiexi),
            bljx:  getStatus(config.bljx),
            qg: getStatus(config.qg),
            ai: getStatus(config.ai),
            bg: await rodom(), //获取底图
        }
        //渲染图像
        return await puppeteer.render("admin/index", {
            ...data,
        }, {
            e,
            scale: 2.0
        });
    }
}

//随机底图
const rodom = async function () {
    var image = fs.readdirSync(`./plugins/xiaoyue-plugin/resources/admin/imgs/bg`);
    var list_img = [];
    for (let val of image) {
        list_img.push(val)
    }
    var imgs = list_img.length == 1 ? list_img[0] : list_img[lodash.random(0, list_img.length - 1)];
    return imgs;
}

const getStatus = function (rote) {
    if (rote) {
        return `<div class="cfg-status" >已开启</div>`;
    } else {
        return `<div class="cfg-status status-off">已关闭</div>`;
    }

}

